package com.zsy.provider.api.feign.hystrix;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsy.base.bean.DemoResult;
import com.zsy.dao.model.CgsDksq;
import com.zsy.dao.model.TbUser;
import com.zsy.provider.api.feign.UacFeignApi;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 回退工厂，可以提供异常，建议使用，方便后期定位异常
 */
@Slf4j
@Component
public class UacFeiginFallbackFactory implements FallbackFactory<UacFeignApi> {
    @Override
    public UacFeignApi create(Throwable throwable) {
        return new UacFeignApi() {
            @Override
            public DemoResult<Page<CgsDksq>> listCgsDksq(JSONObject jsonObject) {
                log.error("获取用户信息出现异常：{}",throwable.getMessage());
                return DemoResult.FAIL("服务发生异常："+throwable);
            }

            @Override
            public DemoResult<Object> listUser(String id) {
                log.error("{}---获取用户信息出现异常：{}",id,throwable.getMessage());
                return DemoResult.FAIL("服务发生异常："+throwable,id);
            }

            /**
             * @param userName
             * @return com.zsy.base.bean.DemoResult<java.lang.Object>
             * @Author zhaoshouyun
             * @Description 获取用户信息
             * @Date 2020-04-15 05:15
             * @Param userName
             */
            @Override
            public DemoResult<TbUser> getUserByUserName(String userName) {
                log.error("{}---通过用户名获取用户信息出现异常：{}",userName,throwable.getMessage());
                return DemoResult.FAIL("服务繁忙，请稍后重试！userName="+userName);
            }

            /**
             * 通过用户名更新用户手机号，为了测试事务
             *
             * @param userName
             * @param phone
             * @return com.zsy.base.bean.DemoResult<com.zsy.dao.model.TbUser>
             * @Author zhaoshouyun
             * @Description
             * @Date 2020-04-29 01:55
             * @Param userName
             * @Param phone
             */
            @Override
            public DemoResult<TbUser> updateUserByUserName(String userName, String phone) {
                log.error("{}---更新用户信息出现异常：{}",userName,throwable.getMessage());
                return DemoResult.FAIL("服务繁忙，请稍后重试！userName="+userName);
            }

            /**
             * 获取权限列表
             *
             * @param id
             * @return com.zsy.base.bean.DemoResult<com.zsy.dao.oauth2.model.TbPermission>
             * @Author zhaoshouyun
             * @Description
             * @Date 2020-04-15 05:36
             * @Param id
             */
            @Override
            public DemoResult selectPermissionsByUserId(BigDecimal id) {
                log.info("服务熔断和降级id：{}",id);
                return DemoResult.FAIL("服务繁忙，请稍后重试！id="+id);
                //return new ArrayList<>();
            }
        };
    }
}
